﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Method_of_least_squares.Models
{
    /// <summary>
    /// 线性回归模型
    /// </summary>
    internal class LinearRegressionModel
    {
        public static double LinearRegression(List<double> year, List<double> GDP,double x)
        {
            //定义平均数average
            double aveG = year.Average();
            double aveY = GDP.Average();
            //定义分子分母
            double numerator=0;
            double denominator=0;
            //定义方程要素
            double a = 0;
            double b = 0;
            double y = 0;
            //计算分子分母
            for(int i = 0; i < 10; i++)
            {
                numerator += (year[i] - aveY)*(GDP[i] - aveG);
                denominator += (year[i] - aveY) * (year[i] - aveY);
            }
            //计算系数
            b=numerator/denominator;
            a = aveG - b * aveY;
            //得到方程
            y= a + b * x;
            return y;
        }

    }
}
